package com.jfinal.demo.config;

import java.util.Properties;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.demo.core.entity.Member;
import com.jfinal.demo.web.controller.FileController;
import com.jfinal.demo.web.controller.HelloController;
import com.jfinal.demo.web.controller.LoginController;
import com.jfinal.demo.web.controller.MemberController;
import com.jfinal.demo.web.interceptor.SessionInterceptor;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.tx.TxByRegex;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;

/**
 * jfinal 配置
 * 
 * @author Administrator
 * 
 */
public class AppConfig extends JFinalConfig {

	private static Properties APP_PRO;

	@Override
	public void configConstant(Constants me) {
		APP_PRO = loadPropertyFile("appConfig.properties");
		me.setDevMode(getPropertyToBoolean("devMode"));
		me.setViewType(ViewType.JSP);
		me.setBaseViewPath("/WEB-INF/views/");
		me.setUploadedFileSaveDirectory(getProperty("fileSavePath"));
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", LoginController.class);
		me.add("/hello", HelloController.class);
		me.add("/file", FileController.class);
		me.add("/member", MemberController.class);
	}

	@Override
	public void configPlugin(Plugins me) {
		loadPropertyFile("jdbc.properties");
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbc.url"),
				getProperty("jdbc.username"), getProperty("jdbc.password"),
				getProperty("jdbc.driverClass"));
		c3p0Plugin.setInitialPoolSize(getPropertyToInt("c3p0.initialPoolSize"));
		c3p0Plugin.setMaxIdleTime(getPropertyToInt("c3p0.maxIdleTime"));
		c3p0Plugin.setMaxPoolSize(getPropertyToInt("c3p0.maxPoolSize"));
		c3p0Plugin.setMinPoolSize(getPropertyToInt("c3p0.minPoolSize"));
		me.add(c3p0Plugin);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		arp.setDialect(new MysqlDialect());
		arp.setShowSql(true);
		me.add(arp);

		// 缓存
		EhCachePlugin ecp = new EhCachePlugin();
		me.add(ecp);

		// 配置实体映射
		arp.addMapping("member", Member.class);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// 会话管理
		me.add(new SessionInterceptor());
		// 事务管理
		me.add(new TxByRegex(".*save.*"));
		me.add(new TxByRegex(".*update.*"));
		me.add(new TxByRegex(".*delete.*"));
	}

	@Override
	public void configHandler(Handlers me) {
	}

	public static String get(String key) {
		String value = APP_PRO.getProperty(key);
		if (value == null) {
			throw new RuntimeException(key + " is not config!");
		}
		return value;
	}

	public static String get(String key, String defaultValue) {
		String value = APP_PRO.getProperty(key);
		if (value == null) {
			throw new RuntimeException(key + " is not config!");
		}
		return value;
	}
}
